﻿using System;
using SharpKit.JavaScript;

namespace SharpKit.Rx.Demo
{
	[JsType(JsMode.Prototype, Filename = "Demo1.js")]
	public static class Demo1
	{
		private static IDisposable SubscribeToConsole<T>(this IObservable<T> observable, JsFunc<T, string> func = null)
		{
			return observable.Subscribe(
				value => Console.WriteLine("next: " + (func == null ? value.As<string>() : func(value))),
				err => Console.WriteLine("error: " + err.As<string>()),
				() => Console.WriteLine("complete."));
		}

		public static void GenerateSample()
		{
			Console.WriteHeader("Generate");
			var observable = Observable.Generate(0, i => i < 10, i => i + 1, i => i);
			observable.SubscribeToConsole();
		}
		public static void GenerateWithTimeSample()
		{
			Console.WriteHeader("GenerateWithTime");
			var observable = Observable.GenerateWithTime(0, i => i < 10, i => i + 1, i => i, i => i * 200).Timestamp();
			observable.SubscribeToConsole(i => i.Value + " on " + new JsDate(i.Timestamp));
		}
		public static void RepeatSample()
		{
			Console.WriteHeader("Repeat(1, 3)");
			var observable = Observable.Repeat(1, 3);
			observable.SubscribeToConsole();
		}
		public static void jQueryClickSample()
		{
			Console.WriteHeader("jQuery click sample");
			Console.WriteLine("click on any point of document, please");
			var observable = jQuery.jQueryContext.J(Html.HtmlContext.document).toObservable("click");
			observable.SubscribeToConsole(i => "x: " + i.offsetX + " y: " + i.offsetY);
		}
		public static void DragSample()
		{
			Console.WriteHeader("Drag sample");
			Console.WriteLine("down mouse on any point of document, please, and then move");
			var d = jQuery.jQueryContext.J(Html.HtmlContext.document);
			var md = d.toObservable("mousedown");
			var mm = d.toObservable("mousemove");
			var mu = d.toObservable("mouseup");
			var observable = mm.SkipUntil(md).TakeUntil(mu);
			observable.SubscribeToConsole(i => "mouse move x: " + i.offsetX + " y: " + i.offsetY);
		}
	}
}